list gMenu=[];
list statesMenu=[0,0,0,0];
list gAvatarAllow;
key ToucherID;
integer gMenuLink =1337;
integer gDemon =1338;
integer gPlugin =1339;
integer gVic =1329;
integer channel_dialog;
integer listen_id;
string msg;
list distance =["2","5","10","15","20"];
displayMenu()
{
	gMenu=[];
	if(llList2Integer(statesMenu,0)==1)gMenu+="[x]Range";
	else gMenu+="[ ]Range";
	gMenu+="Back";
	gMenu+=distance;
	msg = "Pick one";
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	llDialog(ToucherID, msg,gMenu, channel_dialog);
	
}

RequestPerms()
{
    if (llGetAttached())
    {
        llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION);
    }
}

llLRP(integer element_new, integer placeinlist)
{
	statesMenu = llListReplaceList(statesMenu, [element_new], placeinlist, placeinlist);
}

default {
    state_entry() 
    {
    	RequestPerms();
    }
     listen(integer channel, string name, key id, string message)
    {
    	if(channel==channel_dialog)
    	{
			llListenRemove(listen_id);
			if(message=="[x]Range")
			{
				llLRP(0,0);
				llSensorRepeat("", ToucherID, AGENT, 60.0, PI, 20.0);
				displayMenu();
			}
			else if(message=="[ ]Range")
			{
				llLRP(1,0);
				llSensorRepeat("", ToucherID, AGENT, 5.0, PI, 20.0);
				displayMenu();
			}
			else if(message=="Back")
			{
				llMessageLinked(LINK_THIS, gMenuLink, "DV_MAIN_MISC", ToucherID);
			}
			else if(~llListFindList(distance, (list)message))
	  		{
	  			llSensorRepeat("", ToucherID, AGENT, 5.0, PI, (float)message);
	  			displayMenu();
	  		}
    	}
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				displayMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
  			}
  		}
  	}
  	
  	on_rez(integer num)
    {
        RequestPerms();        
    }
  	
  	sensor(integer num_detected)
    {
    	
    }
    
    no_sensor() 
    {
    	if(llList2Integer(statesMenu,0)==1)
    	{
    		if(llGetPermissions() & PERMISSION_TRIGGER_ANIMATION)
			{
				llStartAnimation("shock");
				llSetTimerEvent(5.0);
			}
    	}
    }
  	
  	timer()
  	{
  		llSetTimerEvent(0.0);
  		llStopAnimation("shock");
  	}
}
